package com.agricultural.handler.impl;

import com.agricultural.equipment.AgriculturalEquipment;
import com.agricultural.handler.SafetyHandler;
import com.agricultural.state.impl.MaintenanceState;

// 燃油水平处理器
public class FuelLevelHandler implements SafetyHandler {
    private SafetyHandler nextHandler; // 下一个处理器
    
    // 设置下一个处理器
    @Override
    public void setNextHandler(SafetyHandler nextHandler) {
        this.nextHandler = nextHandler;
    }
    
    // 处理安全事件
    @Override
    public boolean handleSafetyEvent(AgriculturalEquipment equipment, String eventType, double parameter) {
        if ("lowFuel".equals(eventType)) {
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 燃油水平处理器接收到事件");
            
            if (parameter < 20.0) { // 如果燃油水平低于20%
                System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 警告: 燃油不足 (" + parameter + "%)");
                
                // 停止设备并进入维护状态
                equipment.stop();
                equipment.updateState(new MaintenanceState(equipment));
                
                return true; // 事件已处理
            }
        }
        
        // 如果无法处理，传递给下一个处理器
        if (nextHandler != null) {
            return nextHandler.handleSafetyEvent(equipment, eventType, parameter);
        }
        
        return false; // 没有处理器能够处理此事件
    }
}    